package com.extension.company.model.main;

import android.content.Context;
import android.util.Log;

import com.extension.company.Constant;
import com.extension.company.model.entity.HotDataListBean;
import com.extension.company.model.entity.TokenBean;
import com.extension.company.model.entity.ListHeadBean;
import com.extension.company.contract.main.FragmentHomeContract;
import com.extension.company.common.http.RetrofitHelper;
import com.extension.company.model.api.RetrofitUrlApi;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 首页fragment的M层
 */
public class FragmentHomeMode implements FragmentHomeContract.Mode {
	private RetrofitUrlApi retrofitUrlApi = RetrofitHelper.getRetrofit().create(RetrofitUrlApi.class);

	@Override
	public void requestDataOne(Context context, final FragmentHomeContract.FragmentHomeModelCallback callback) {

		TokenBean tokenBean = new TokenBean(Constant.SIGN, "123456"); // Post 请求上传的数据
		retrofitUrlApi.postHeader(tokenBean)
				.subscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Observer<ListHeadBean>() {
					@Override
					public void onSubscribe(Disposable d) {

					}

					@Override
					public void onNext(ListHeadBean listHeadBean) {
						if (listHeadBean.getCode() == 200) { // 	请求成功
							if (null != listHeadBean.getData()) {

								if (null != listHeadBean.getData().getBanner() && listHeadBean.getData().getBanner().size() > 0) {
									callback.banner(listHeadBean.getData().getBanner());  // 将banner数据单独的返回
								}

								if (null != listHeadBean.getData().getOther_ad() && listHeadBean.getData().getOther_ad().size() > 0) {
									callback.adList(listHeadBean.getData().getOther_ad());   // 三个广告位的回调
								}

								if (null != listHeadBean.getData().getAd_data()) {  // 单个广告页
									callback.adImage(listHeadBean.getData().getAd_data());
								}

								if (null != listHeadBean.getData().getFirst_data() && listHeadBean.getData().getFirst_data().size() > 0) {
									callback.activityText(listHeadBean.getData().getFirst_data());
								}
								if (null != listHeadBean.getData().getCate_list() && listHeadBean.getData().getCate_list().size() > 0) {
									callback.catList(listHeadBean.getData().getCate_list());
								}
							}

						}

					}

					@Override
					public void onError(Throwable e) {

					}

					@Override
					public void onComplete() {

					}
				});
	}


	@Override
	public void requestHotData(final FragmentHomeContract.HotDataCallBack hotDataCallBack) { // 请求热门活动
		retrofitUrlApi
				.postHotData(Constant.SIGN)
				.observeOn(AndroidSchedulers.mainThread())
				.subscribeOn(Schedulers.io())
				.subscribe(new Observer<HotDataListBean>() {
					@Override
					public void onSubscribe(Disposable d) {

					}

					@Override
					public void onNext(HotDataListBean hotDataListBean) {
						Log.d("报错", "============接收到了数据" + hotDataListBean.toString());
						if (200 == hotDataListBean.getCode() && null != hotDataListBean.getData().getHot_data()) {
							hotDataCallBack.hotList(hotDataListBean.getData().getHot_data());
						}

					}

					@Override
					public void onError(Throwable e) {
						Log.d("报错", e.getMessage());

					}

					@Override
					public void onComplete() {

					}
				});
	}


}
